package edu.zjut.hjg.saas.kwmp.web.controller.teacher;

import static edu.zjut.hjg.saas.kwmp.web.support.KwmpConstants.SESSION_NAME_USER;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.model.News;
import edu.zjut.hjg.saas.biz.service.NewsService;
import edu.zjut.hjg.saas.common.annotation.CookieSessionIn;
import edu.zjut.hjg.saas.common.annotation.RequestAttribute;
import edu.zjut.hjg.saas.common.util.ParameterUtil;
import edu.zjut.hjg.saas.kwmp.web.model.UserSession;

@Controller
@RequestMapping("/teacher/news")
public class NewsManageController {

    @Autowired
    private NewsService newsService;

    @CookieSessionIn(value = SESSION_NAME_USER, param = "session")
    @RequestMapping(value = "create.htm", method = RequestMethod.POST)
    public String create(@RequestAttribute Kindergarten app, UserSession session, Model model,
                         String content, String title) {
        if (!ParameterUtil.notBlank(content, title)) {
            model.addAttribute("errorMsg", "请新闻标题和内容都不能为空");
            model.addAttribute("content", content);
            model.addAttribute("title", title);
            return "teacher/news";
        }
        News news = new News();
        news.setContent(content);
        news.setTitle(title);
        news.setKindergarten(app);
        news.getUser().setId(session.getId());
        newsService.create(news, session.getLoginId());
        model.addAttribute("message", "添加新闻成功");
        return "teacher/news";
    }
}
